package com.game.doudizhu.service;

import com.game.doudizhu.controller.FriendController;
import com.game.doudizhu.model.Friendship;
import com.game.doudizhu.model.Player;
import com.game.doudizhu.repository.FriendshipRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class FriendshipService {
    
    @Autowired
    private FriendshipRepository friendshipRepository;
    
    @Transactional
    public Friendship sendFriendRequest(Player sender, Player receiver) {
        if (receiver == null) {
            throw new RuntimeException("用户不存在");
        }

        if (sender.getId().equals(receiver.getId())) {
            throw new RuntimeException("不能添加自己为好友");
        }
        Friendship friendship = friendshipRepository.findByPlayerAndFriend(sender, receiver);
        if(friendship != null) {
            if(friendship.getStatus().equals("PENDING")) {
                throw new RuntimeException("已经发送过好友请求");
            } else if (friendship.getStatus().equals("ACCEPTED")) {
                throw new RuntimeException("已经是好友关系");
            }
        }
        friendship = new Friendship();
        friendship.setPlayer(sender);
        friendship.setFriend(receiver);
        friendship.setCreatedAt(LocalDateTime.now());
        friendship.setStatus("PENDING");
        
        return friendshipRepository.save(friendship);
    }
    
    @Transactional
    public Friendship acceptFriendRequest(Long requestId) {
        Friendship friendship = friendshipRepository.findById(requestId)
            .orElseThrow(() -> new RuntimeException("好友请求不存在"));
            
        if (!"PENDING".equals(friendship.getStatus())) {
            throw new RuntimeException("该请求已处理");
        }
            
        friendship.setStatus("ACCEPTED");
        
        // 创建双向好友关系
        Friendship reverseFriendship = new Friendship();
        reverseFriendship.setPlayer(friendship.getFriend());
        reverseFriendship.setFriend(friendship.getPlayer());
        reverseFriendship.setStatus("ACCEPTED");
        reverseFriendship.setCreatedAt(LocalDateTime.now());
        
        friendshipRepository.save(reverseFriendship);
        return friendshipRepository.save(friendship);
    }
    
    @Transactional
    public void rejectFriendRequest(Long requestId) {
        friendshipRepository.deleteById(requestId);
    }
    
    @Transactional
    public void removeFriend(Player player, Player friend) {
        // 删除双向的好友关系
        friendshipRepository.deleteByPlayerAndFriendAndStatus(player, friend, "ACCEPTED");
        friendshipRepository.deleteByPlayerAndFriendAndStatus(friend, player, "ACCEPTED");
    }
    
    public List<Player> getFriendList(Player player) {
        return friendshipRepository.findFriendsByPlayerAndStatus(player, "ACCEPTED");
    }
    
    public List<Friendship> getPendingRequests(Player player) {
        return friendshipRepository.findByFriendAndStatus(player, "PENDING");
    }
    
    public boolean areFriends(Player player1, Player player2) {
        return friendshipRepository.existsByPlayerAndFriendAndStatus(player1, player2, "ACCEPTED");
    }
} 